package com.app.ytong.manager.in;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.app.ytong.R;
import com.app.ytong.basis.BasisActivity;
import com.app.ytong.basis.Constants;
import com.app.ytong.basis.MyViewPagerAdapter;
import com.app.ytong.bean.GoodsListBean;
import com.app.ytong.bean.GoodsListData;
import com.app.ytong.bean.LoginBean;
import com.app.ytong.bean.MenuBean;
import com.app.ytong.greendao.GreenDaoUtils;
import com.app.ytong.login.LoginActivity;
import com.app.ytong.multiselection.TypeLabelGridLayout;
import com.app.ytong.record.RecordListActivity;
import com.app.ytong.util.MyUtils;
import com.app.ytong.util.SplitUtil;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;
import com.vandin.weigher.sdk.connector.IWeigh;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.AnimationTool;
import my.DialogUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

/**
 * 入库管理
 */
public class PutInActivity extends BasisActivity implements PutView {


    @BindView(R.id.view_goods_menu)
    RelativeLayout view_goods_menu;
    @BindView(R.id.view_multi)
    ScrollView view_multi;

    @BindView(R.id.img_add)
    ImageView img_add;


    private IWeigh IControl;
    PutPresenter presenter;


    @BindView(R.id.tabSegment)
    QMUITabSegment mTabSegment;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    MyViewPagerAdapter myViewPagerAdapter;


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    GoodsMenuAdapter goodsMenuAdapter;
    GoodsListBean mListBean;

    @BindView(R.id.lgl_label)
    TypeLabelGridLayout lglLabel;

    @Override
    public void initViews() {
        setContentView(R.layout.put_in_activity);
        setTitleBg(R.color.transparent);
        setTitle("入存管理");
        setTitleLeftButton(null);
        setTitleRightButton(R.mipmap.icon_exit_red, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginActivity.toLoginAllClear(mContext);
            }
        });



        presenter = new PutPresenterImpl(this,mContext);
        IControl = presenter.getIWeigh();
        presenter.initLglLabel(lglLabel);

        MenuBean menuData = LoginBean.getInstance().getRightById(MyUtils.getHomeSelectId()).getIsSeleChild();
        if(menuData == null) return;

        Fragment[] fms = new Fragment[menuData.getChild().size()];
        

        for(int i = 0;i<menuData.getChild().size();i++){
            MenuBean data = menuData.getChild().get(i);
            fms[i] = PutInFragment.newInstance(data.getId(),data.getSna(),presenter);
        }
        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fms);
        viewPager.setAdapter(myViewPagerAdapter);
        viewPager.setOffscreenPageLimit(menuData.getChild().size());
        presenter.initTabSegment(menuData,mTabSegment,viewPager,view_goods_menu);


        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);

        setTitleRightButton2(R.mipmap.icon_tran_record, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MenuBean data = menuData.getChild().get(viewPager.getCurrentItem());
                MyUtils.putTabSelectId(data.getId());
                MyUtils.putTabSelectName(data.getSna());
                RecordListActivity.toRecordListActivity(mContext,"other");
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        setTitleBg(R.color.transparent);
        QMUIStatusBarHelper.setStatusBarDarkMode(mContext);
        mListBean = new GoodsListBean();
        setListView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.closeIWeigh();
    }

    @OnClick(R.id.tv_confirm)
    public void confirmBtn(){
        view_multi.setVisibility(View.GONE);
        getList();
    }
    @OnClick(R.id.tv_reset)
    public void resetBtn(){
        lglLabel.resetData();
    }
    @OnClick(R.id.img_add)
    public void showMultiple(){
        AnimationTool.inOrOut(mContext,view_goods_menu);
        view_multi.setVisibility(View.VISIBLE);
    }


    private void setListView() {
        if (goodsMenuAdapter == null) {
            goodsMenuAdapter = new GoodsMenuAdapter();
            mRecyclerView.setAdapter(goodsMenuAdapter);
        } else {
            goodsMenuAdapter.notifyDataSetChanged();
        }
    }

    private void getList() {
        MyParams params = new MyParams();
        showProgress();
        params.put("aid", LoginBean.getInstance().getAid());
        params.put("ifs", MyUtils.getHomeSelectId());
        if(lglLabel != null && lglLabel.getLabelData().size()>0){
            for(String mul :lglLabel.getLabelData()){
                if(mul.contains("商品类型") && !mul.contains("不限")){
                    params.put("tid", GreenDaoUtils.getGoodsTypeListBean().getTidByName(SplitUtil.split(mul,"-",1)));
                } if(mul.contains("商品等级")&& !mul.contains("不限")){
                    params.put("lid", GreenDaoUtils.getGoodsLevelListBean().getLidByName(SplitUtil.split(mul,"-",1)));
                }if(mul.contains("商品年份")&& !mul.contains("不限")){
                    params.put("yid", GreenDaoUtils.getGoodsTimeListBean().getYidByName(SplitUtil.split(mul,"-",1)));
                }
            }
        }

        MyHttpUtils.get(Constants.URL_GOODS_SELECT_ALL, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        mListBean= (GoodsListBean) result;
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        super.onFailure(httpWhat, result);
                        setListView();
                        mListBean.getList().clear();
                        showMultiple();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        if (isFinishing()) return;
                        dismissProgress();
                    }
                },
                0, GoodsListBean.class);
    }




    @Override
    public void showWeight(float weight) {

    }

    @Override
    public void printMessage(String message) {
        Fragment childFragment = myViewPagerAdapter.getItem(viewPager.getCurrentItem());
        if(childFragment instanceof PutInFragment){
            PutInFragment fragment = (PutInFragment)childFragment;
            fragment.printMessage(message);
        }
    }

    @Override
    public void printFinish() {

    }

    @Override
    public void httpSuccess() {
        Fragment childFragment = myViewPagerAdapter.getItem(viewPager.getCurrentItem());
        if(childFragment instanceof PutInFragment){
            PutInFragment fragment = (PutInFragment)childFragment;
            fragment.httpSuccess();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            myViewPagerAdapter.getItem(viewPager.getCurrentItem()).onActivityResult(requestCode,resultCode,data);
    }

    public class GoodsMenuAdapter extends RecyclerView.Adapter<GoodsMenuAdapter.ViewHolder> {
        public GoodsMenuAdapter() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.put_in_item, viewGroup, false);
            return new ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_gsp)
            public TextView text_gsp;
            @BindView(R.id.text_unit)
            public TextView text_unit;
            @BindView(R.id.text_gno)
            public TextView text_gno;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                GoodsListData data = mListBean.getList().get(position);
                MyUtils.setNotNullText(text_name,data.getGna());
                MyUtils.setNotNullText(text_gsp,data.getGsp());
                MyUtils.setNotNullText(text_unit,data.getGun());
                MyUtils.setNotNullText(text_gno,data.getGno());
                list_item.setTag(data);
            }
            @OnClick(R.id.list_item)
            void onItemClick(View view){
                showMultiple();
                GoodsListData data = (GoodsListData) view.getTag();
                data.setGwt(presenter.getScaleWeight());
                Fragment childFragment = myViewPagerAdapter.getItem(viewPager.getCurrentItem());
                if(childFragment instanceof PutInFragment){
                    PutInFragment fragment = (PutInFragment)childFragment;
                    fragment.setData(data);
                }
            }
        }



    }
}